/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.code.michaelkzhang.bp.InnKeeper;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;

import java.util.logging.*;

/**
 *
 * @author shengzhang
 */
public class InnKeeperPlayerListener implements Listener {

    /**
     * Our main plugin
     */
    private final InnKeeper plugin;
    /**
     * Our logger
     */
    @SuppressWarnings("NonConstantLogger")
    private final Logger log;

    /**
     *
     * @param innkeeper
     */
    @SuppressWarnings("LeakingThisInConstructor")
    public InnKeeperPlayerListener(InnKeeper innkeeper) {
        plugin = innkeeper;
        log = innkeeper.getLogger();
        innkeeper.getServer().getPluginManager().registerEvents(this, innkeeper);
    }

    /**
     * Called when the player interacts with the world.
     *
     * @param event The event
     */
    public void onPlayerInteract(PlayerInteractEvent event) {
        if (!event.hasBlock()) {
            return;
        }

        Action action = event.getAction();
        Player player = event.getPlayer();
        Block block = event.getClickedBlock();
        int type = block.getTypeId();
        BlockFace face = event.getBlockFace();

        if (type != Material.WOODEN_DOOR.getId() && type != Material.IRON_DOOR.getId()) {
            return;
        }
        Sign sign = getSign(findSign(block));
        if (sign == null) {
            // No sign found
            return;
        }

        String[] text = sign.getLines();
        prepareText(text);
        if (!player.getName().startsWith(text[3])) {
            event.setUseInteractedBlock(Event.Result.DENY);
            event.setUseItemInHand(Event.Result.DENY);
        } else {
            
        }
    }

    /**
     * Finds the sign associated with the given block.
     *
     * @param block The block
     * @return The sign
     */
    private static Block findSign(Block block) {
        Block sBlock = block.getRelative(BlockFace.UP);
        for (BlockFace face : InnKeeper.SEARCH_ORDER) {
            Block checkBlock = sBlock.getRelative(face);
            if (isSign(checkBlock)) {
                return checkBlock;
            }
        }
        return null;
    }

    /**
     * Checks if the block is a sign.
     *
     * @param cBlock The block to check
     * @return If the block is a sign
     */
    private static boolean isSign(Block cBlock) {
        switch (cBlock.getType()) {
            case SIGN:
            case WALL_SIGN:
            case SIGN_POST:
                return true;
            default:
                return false;
        }
    }

    /**
     * 
     * @param lines
     */
    private static void prepareText(String[] lines) {
        for (int i = 0; i < lines.length; ++i) {
            lines[i] = lines[i].replaceAll("(?i)\u00A7[0-F]", "");
        }
    }

    /**
     * Returns the block as a sign, otherwise {@code null}.
     *
     * @param block The block to cast
     * @return The sign
     */
    private static Sign getSign(Block block) {
        try {
            return (Sign) block.getState();
        } catch (ClassCastException cce) {
        } catch (NullPointerException npe) {
        }
        return null;
    }
}
